﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatherWebLib;
using GatherWebLib.Model;
using System.Reflection;

namespace WebGet_Win
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private cGatherTask AddRunTask(string tClassName, string tName)
        {
            cTaskRun tr = new cTaskRun();
            cTaskClass tc = new cTaskClass();//任务分类
            string tPath = "";

            if (tClassName == "")
            {
                tPath = cXmlSConfig.TaskPath + "tasks";
            }
            else
            {
                tPath = tc.GetTaskClassPathByName(tClassName);
            }
            return null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cTask task = new cTask();
            cTaskClass tc = new cTaskClass();//任务分类
            string tClassName = "小说修改";
            string tName = "小说列表";
            string tPath = "";

            if (tClassName == "")
            {
                tPath = cXmlSConfig.TaskPath + "tasks";
            }
            else
            {
                tPath = tc.GetTaskClassPathByName(tClassName);
            }
           tc = null;
           string tFileName = tPath + "\\" + tName + ".xml";
           cTask_m m = task.LoadTask(tFileName);
           cGatherTask Gather = new cGatherTask(m);
           Gather.Log+=tGather_Log;
           DataTable dt= Gather.Start();


        }



        //写日志事件
        private void tGather_Log(object sender, cGatherTaskLogArgs e)
        {
            //写日志
            string TaskName = e.TaskName;
            string strLog = e.strLog;
         

            try
            {
                InvokeMethod(this, "ShowInfo", new object[] { e.TaskName, strLog });

            }
            catch (System.Exception ex)
            {
                InvokeMethod(this, "ExportLog", new object[] { DateTime.Now + "：" + ex.Message });
            }

        }
        public void ShowInfo(string Title, string Info)
        {

            ExportLog(DateTime.Now + "：" + "<" + Title + ">    " + Info);

        }
        public void ExportLog(string str)
        {


           
                try
                {
                    cSystemLog sl = new cSystemLog();
                    sl.WriteLog(str);
                    sl = null;
                }
                catch (System.Exception ex)
                {
                    InvokeMethod(this, "ExportLog", new object[] { ex.Message });
                }
            
        }

        #region 委托代理

        delegate object invokemethod(object Instance, string Method, object[] parameters);

        /// <summary>
        /// 委托执行实例的方法，方法必须都是Public 否则会出错
        /// </summary>
        /// <param name="Instance">类实例</param>
        /// <param name="Method">方法名</param>
        /// <param name="parameters">参数列表</param>
        /// <returns>返回值</returns>
        private object InvokeMethod(object Instance, string Method, object[] parameters)
        {
            if ((bool)GetPropertyValue(Instance, "InvokeRequired"))
            {
                invokemethod d = new invokemethod(InvokeMethod);
                return this.Invoke(d, new object[] { Instance, Method, parameters });
            }
            else
            {
                return MethodInvoke(Instance, Method, parameters);
            }
        }
        /// <summary>
        /// 执行实例的方法
        /// </summary>
        /// <param name="ClassInstance">类实例</param>
        /// <param name="MethodName">方法名</param>
        /// <param name="parameters">参数列表</param>
        /// <returns>返回值</returns>
        private static object MethodInvoke(object ClassInstance, string MethodName, object[] parameters)
        {
            if (parameters == null)
            {
                parameters = new object[0];
            }
            Type myType = ClassInstance.GetType();
            Type[] types = new Type[parameters.Length];
            for (int i = 0; i < parameters.Length; ++i)
            {
                types[i] = parameters[i].GetType();
            }
            MethodInfo myMethodInfo = myType.GetMethod(MethodName, types);
            return myMethodInfo.Invoke(ClassInstance, parameters);
        }
        /// <summary>
        /// 获取实例的属性值
        /// </summary>
        /// <param name="ClassInstance">类实例</param>
        /// <param name="PropertyName">属性名</param>
        /// <returns>属性值</returns>
        private static object GetPropertyValue(object ClassInstance, string PropertyName)
        {
            Type myType = ClassInstance.GetType();
            PropertyInfo myPropertyInfo = myType.GetProperty(PropertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            return myPropertyInfo.GetValue(ClassInstance, null);
        }
        #endregion

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}
